﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ClsController;
using ClsUtilities;
using ClsController.CatalogosNegocio;
using System.Windows.Forms.Calendar;
using ClsCustomControls;

namespace Pro_Care_System
{
    public partial class Appointments : Form
    {
        private ClsLogicaNeg logica;
        private ClsLogicaCitas logicacitas;
        private Catalogo.FormMode accionForma;
        
        public Appointments()
        {
            InitializeComponent();
            logica = new ClsLogicaNeg();
            logicacitas = new ClsLogicaCitas();
            asignarCampoBD();
        }

        private void asignarCampoBD()
        {

            txtPaciente.DBFieldName = TableStruct.PATIENT_DESCR_VALUES.PA_NAME.ToString();
            txtAddr.DBFieldName = TableStruct.PATIENT_DESCR_VALUES.PA_ADDRES.ToString();
            txtphone.DBFieldName = TableStruct.PATIENT_DESCR_VALUES.PA_PHONE.ToString();
            txtAgencia.DBFieldName = TableStruct.PATIENT_DESCR_VALUES.AG_NAME.ToString();
            txtAsistente.DBFieldName = TableStruct.PATIENT_DESCR_VALUES.WRK_NAME_AS.ToString();
            lblcodpatient.DBFieldName = TableStruct.PATIENT_DESCR_VALUES.PA_COD_PATIENT.ToString();
            txtTerapeuta.DBFieldName = TableStruct.PATIENT_DESCR_VALUES.WRK_NAME.ToString();
            txtdobdate.DBFieldName = TableStruct.PATIENT_DESCR_VALUES.PA_DOB.ToString();
            cmbfrec.DBFieldName = TableStruct.TblInfo.FRECUENCY.ToString();
            cmbfrec.DBDescColumnBind = TableStruct.FRECUENCY.FRC_DESCR.ToString();
            cmbfrec.DBCodeColumnBind = TableStruct.FRECUENCY.FRC_COD_FREC.ToString();
        }

        private void Appointments_Load(object sender, EventArgs e)
        {
            accionForma = Catalogo.FormMode.FOR_SEARCH;
            cargarComponenetesIniciales();
            bloqueaSeccionCitas();
            bloqueElementosPacientes();
        }

        private void cklweekdays_SelectedIndexChanged(object sender, EventArgs e)
        {
        }

        private void cklweekdays_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            string valorDiasCant = cmbfrec.SelectedValue.ToString();
            valorDiasCant = (valorDiasCant.Split('-'))[0].ToString(); ;
            int cantmaximadias = int.Parse(valorDiasCant);

            if (cklweekdays.CheckedItems.Count < cantmaximadias && e.CurrentValue == CheckState.Unchecked)
            {
            }
            else
            {
                if (e.CurrentValue == CheckState.Unchecked)
                {
                    e.NewValue = e.CurrentValue;
                    MessageBox.Show("CANTIDAD MAXIMA DE DIAS");
                }
            }
        }

        public void limpiaCalByFrec()
        {


        }

        public void limpiaCalByDay()
        { }

        private void button1_Click(object sender, EventArgs e)
        {
            bool result;
            result = logicacitas.GeneraCalendario(txtPaciente);
            if (result)
            {
                btnGeneraCal.Enabled = false;
                //btnSaveCal.Enabled = false;
            }
        }

        private void rbtPattern_CheckedChanged(object sender, EventArgs e)
        {
            if (rbtPattern.Checked == true)
            {
                tabPanelCalendarType.SelectTab(tbpagePatternCal.Name);
            }
        }


        private void tabPanelCalendarType_SelectedIndexChanged(object sender, EventArgs e)
        {
        }

        private void btnbuscar_Click(object sender, EventArgs e)
        {
            bool resultado;
            List<Control> valores  = new List<Control>();
            valores.Add(lblcodpatient);
            valores.Add(txtPaciente);
            valores.Add(txtAgencia);
            valores.Add(txtdobdate);
            valores.Add(txtphone);
            valores.Add(txtAddr);
            valores.Add(txtTerapeuta);
            valores.Add(txtAsistente);

            resultado = logicacitas.CargarPacietnes(txtPaciente, valores);
            if (resultado)
            {
                desbloqueaSeccionCitas();
                inicializarFechasCitas();
                txtPaciente.Enabled =false;
                btnbuscar.Enabled = false;
                btnNewPat.Enabled = false;

            }
        }

        private void cargarComponenetesIniciales()
        {
            List<DBDropDownList> componente = new List<DBDropDownList>();
            componente.Add(cmbfrec);

            List<TableStruct.TblInfo> nomTablas = new List<TableStruct.TblInfo>();
            //SE AGRUPAN LOS NOMBRE DE TABLAS
            nomTablas.Add(TableStruct.TblInfo.FRECUENCY);

            logica.CargarMutliplesComboBoxCatalogosDBField(nomTablas.ToArray(), componente);
            cklweekdays.Items.AddRange(Enum.GetNames(typeof(ClsController.CatalogosNegocio.Catalogo.WeekDays)));
        }

        private void bloqueaSeccionCitas()
        { 
            grpappointment.Enabled = false;
        }

        private void desbloqueaSeccionCitas()
        {
            grpappointment.Enabled = true;
            limpiaElementosPatronCita();
        }

        private void bloqueElementosPacientes()
        {
            txtphone.Enabled = false ;
            txtAddr.Enabled = false;
            txtAgencia.Enabled = false;
            txtAsistente.Enabled = false;
            txtTerapeuta.Enabled = false;
            txtdobdate.Enabled = false;
        }

        private void inicializarFechasCitas()
        {
            dtpStarDate.Value = DateTime.Today;
            dtpReviewDate.Value = DateTime.Today;
            dtpFinishDate.Value = DateTime.Today;

            dtpStarDate.MinDate = DateTime.Today;
            dtpFinishDate.MinDate = dtpStarDate.Value;
            dtpReviewDate.MinDate = dtpStarDate.Value;
        }

        private void limpiaElementosPatronCita()
        {
            cmbfrec.SelectedIndex = 0;
            cklweekdays.Items.Clear();
            cklweekdays.Items.AddRange(Enum.GetNames(typeof(ClsController.CatalogosNegocio.Catalogo.WeekDays)));

        }

        private void CambioValorDateTimeValidacion(object sender, EventArgs e)
        {
            dtpStarDate.MinDate = DateTime.Today;
            dtpFinishDate.MinDate = dtpStarDate.Value;
        }

        private void btnAgregarInfoCal_Click(object sender, EventArgs e)
        {
          bool result;
          result = logicacitas.AgregarFilasTablaCitas(cmbfrec, cklweekdays,dtpStarDate,dtpHoraCita);
          if (result)
              result = logicacitas.GeneraGridCitas(dataGridView1);

          if (result)
              limpiaElementosPatronCita();
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            bool result;
            if (e.ColumnIndex == 0)
            {
                result = logicacitas.ELiminaFilaTablaCitas(e.RowIndex);
                if (result)
                    result = logicacitas.GeneraGridCitas(dataGridView1);
            }
        }

        private void btnSaveCal_Click(object sender, EventArgs e)
        {
            bool result;
            result = logicacitas.GuardarCitas(lblcodpatient, lblcodpatient);

            if (result)
                btnSaveCal.Enabled = false;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            txtPaciente.Text = String.Empty;
            txtPaciente.Enabled = true; 
            lblcodpatient.Text = String.Empty;
            txtphone.Text = String.Empty;
            txtAddr.Text = String.Empty;
            txtAgencia.Text = String.Empty;
            txtAsistente.Text = String.Empty;
            txtTerapeuta.Text = String.Empty;
            txtdobdate.Text = String.Empty;
            bloqueaSeccionCitas();
            btnNewPat.Enabled = true;
            btnbuscar.Enabled = true;
        }

        private void btnClearCalInfo_Click(object sender, EventArgs e)
        {
            logicacitas.LimpiaElementosCalendario();
            btnSaveCal.Enabled = true;
            btnGeneraCal.Enabled = true;
        }
     }
}
